package com.it135.citytraffic.service.impl;

import com.it135.citytraffic.mapper.VisitsMapper;
import com.it135.citytraffic.service.VisitsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.Date;

@Service
public class VisitsServiceImpl implements VisitsService {
    @Autowired
    VisitsMapper visitsMapper;

    @Override
    public Integer getVisitsCount() {
        LocalDate today = LocalDate.now();
        Integer res = visitsMapper.getVisitsCountByDate(java.sql.Date.valueOf(today));
        if (res == null) {
            visitsMapper.addTodayData(java.sql.Date.valueOf(today));
            return 1;
        } else {
            visitsMapper.updateTodayData(res + 1, java.sql.Date.valueOf(today));
            return res;
        }
    }
}
